﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;

using LuxDiner.Model.Accounts;
using LuxDiner.Logic.Accounts;
using LuxDiner.Model.Results;
namespace LuxDiner.Web.Management.Areas.Accounts.Controllers
{
    [RouteArea("Accounts")]
    [RoutePrefix("User")]
    public class UserApiController : ApiController
    {
        [Route("Gets")]
        [HttpPost]
        public JsonEntitySet<UserModel> Gets(JObject request)
        {
            string errinfo = "no data";
            try
            {
                UserQuery sea = new UserQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                sea.Key = request.Value<string>("key");
                var result = UserLogic.GetUsers(sea);
                return new JsonEntitySet<UserModel>(result);
            }
            catch (Utils.LuxDinerException lde) {
                errinfo = lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<UserModel>(null, errinfo, false);
        }

        [Route("Save")]
        [HttpPost]
        public JsonEntity<UserModel> Save(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                #region 收集参数
                UserModel model = new UserModel() { Roles = new RoleCollection() };
                model.ID = request.Value<int>("userID");
                model.UserName = request.Value<string>("username");
                model.Password = request.Value<string>("password");
                model.RealName = request.Value<string>("realname");
                model.Mobile = request.Value<string>("mobile");
                model.Status = request.Value<int>("status");
                var roles = request.Value<string>("roles");
                var roleArray = roles.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (roleArray.Length > 0)
                {
                    foreach (var c in roleArray)
                    {
                        model.Roles.Add(new RoleModel() { ID = int.Parse(c) });
                    }
                }
                #endregion

                if (model.ID == 0)
                    model = UserLogic.CreateNewUser(model);
                else
                    UserLogic.UpdateUser(model);
                return new JsonEntity<UserModel>(model);
            }
            catch (Utils.LuxDinerException lde)
            {
                errinfo += lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntity<UserModel>(null, errinfo, false);
        }

        [Route("Delete")]
        [HttpPost]
        public JsonResult<bool> Delete(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                #region 收集参数
                var id = request.Value<int>("userID");
                #endregion
                UserLogic.DeleteUser(id);
                return new JsonResult<bool>(true);
            }
            catch (Utils.LuxDinerException lde)
            {
                errinfo += lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
    }
}
